package cn.wsalix.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.anlaser.admin.BaseUser;
import cn.wsalix.exception.UserException;
@Component("userUtils")
public class UserUtils {
	@Autowired
	protected UserException userException;
	public BaseUser getUser() {
		try {
			Subject subject = SecurityUtils.getSubject();
			BaseUser user = (BaseUser) subject.getPrincipal();
			return user;
		} catch (UnavailableSecurityManagerException ex) {
			return null;
		}

	}

	public BaseUser getInUser() {
		try {
			Subject subject = SecurityUtils.getSubject();
			BaseUser user = (BaseUser) subject.getPrincipal();
			if (user == null) {
				throw new AuthenticationException();
			}
			return user;
		} catch (UnavailableSecurityManagerException ex) {
			return null;
		}

	}
}
